// pages/addTodo/addTodo.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        classify:[],
        judge:[], //判断当前分类是否被选中
        curYear: new Date().getFullYear(), 
        curMonth: new Date().getMonth() + 1,
        day: new Date().getDate(),
        today:'',
        hasTitle:false,
        hasTime:false,
        showCal:false,
        selectDate:'',
        newOrSelect:false, //false为选择已有，true为新建
        selectClassify:'',
        title:'',
        desc:'',
        inClassify:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            today:this.data.curYear.toString()+'-'+this.data.curMonth.toString()+'-'+this.data.day.toString()
        })
        var that=this
        wx.cloud.callFunction({
            name: 'getClassification',
            success:function (res) {
              that.setData({
                classify:res.result,
                judge:new Array(res.result.length).fill(0)
              })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    confirmClassify:function () {
        if(this.data.newOrSelect==false)
            return this.data.selectClassify
        else{
            return this.data.inClassify
        }  
    },

    done:function () {
        var subClassify=this.confirmClassify()
        var that=this
        var subTime=''
        if(this.data.hasTime==true){
            subTime=Number(new Date(this.data.selectDate))
        }
        else{
            subTime=Number(new Date(this.data.today))
        }
        if(subClassify==''&&this.data.title==''){
            wx.showModal({
                title: '提示',
                content: '请输入标题并选择列表后再试',
            })
        }
        else if(subClassify==''&&this.data.title!=''){
            wx.showModal({
                title: '提示',
                content: '请选择列表后再试',
            })
        }
        else if(subClassify!=''&&this.data.title==''){
            wx.showModal({
                title: '提示',
                content: '请输入标题后再试',
            })
        }
        else{
            var checkStr=that.data.title+' '+that.data.desc+' '+subClassify
            wx.cloud.callFunction({
                name: 'msgCheck',
                data:{
                  content:checkStr
                },
                success:function (res1) {
                    if(res1.result.result.suggest==="pass"){
                        wx.cloud.callFunction({
                            name: 'addClassification',
                            data:{
                                title:that.data.title, 
                                desc:that.data.desc,
                                time:subTime,
                                hasTime:that.data.hasTime,
                                classify:subClassify
                            },
                            success:function (res) {
                              wx.navigateBack({
                                  delta:1
                              })
                            }
                        })
                    }
                    else{
                        wx.showModal({
                            title: '提示',
                            content: '当前输入包含违规内容，请重新输入后再试',
                            success: function() {
                                that.onShow()
                            }
                        })
                    }
                }
            })

            
        }
    },

    inputClassify:function (e) {
        this.setData({
            inClassify:e.detail.value
        })
    },

    recTitle:function (e) {
        this.setData({
            title:e.detail.value
        })
    },

    recDesc:function (e) {
        this.setData({
            desc:e.detail.value
        })
    },

    selectClassify:function (e) {
      var c=e.currentTarget.dataset.class
      var idx=e.currentTarget.dataset.idx
      var m='judge['+idx+']'
      this.setData({
          selectClassify:c,
          judge:new Array(this.data.judge.length).fill(0),
          [m]:1
      })
    },

    newOrSelect:function () {
      this.setData({
          newOrSelect:!this.data.newOrSelect
      })  
    },

    showCal:function () {
        this.setData({
            showCal:!this.data.showCal,
        })
        if(this.data.selectDate==''&&this.data.hasTime==false){
        }
        else {
            this.setData({
                selectDate:'',
                hasTime:false
            })
        }   
    },

    selectDate:function (e) {
        const date=e.detail.date
        this.setData({
            selectDate:date,
            hasTime:true
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return{
            title:'',
            path:'/pages/index/index',
            imageUrl:'/images/icon.png',
            success:(res)=>{
              
            }
          }
    }
})
